package com.buba.system.controller;

import com.buba.system.pojo.Employee;
import com.buba.system.pojo.Menu;
import com.buba.system.pojo.Page;
import com.buba.system.pojo.SystemUser;
import com.buba.system.service.JobService;
import com.buba.system.service.LoginService;
import com.buba.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Menu")
public class MenuController {
    private LoginService loginService;
    private MenuService menuService;
    private JobService jobService;
    @RequestMapping("/getMenu")
    public Map<String, Employee> getMenu(HttpSession session) {
        Employee employee = (Employee) session.getAttribute("employee");
        Map<String, Employee> map = new HashMap<>();
        map.put("employee", employee);
        return map;
    }
    //分页查询所有菜单信息
    @RequestMapping("/selMenuLimit")
    public Page selMenuAlll(@RequestParam Map<String, Object> map){
        Page page = new Page((Integer.parseInt(map.get("pageNow").toString())),(Integer.parseInt(map.get("pageCount").toString())));
        map.put("page", page);
        List<Menu> list = null;
        //查询菜单总条数
        page.computePageTotal(menuService.selMenuCount());
        //查询所有菜单
        list = menuService.selMenuAlll(page);
        //把list装入
        page.setMenuList(list);
        return page;
    }

    @RequestMapping("/updpas")
    public Map<String, Object> updpas(HttpSession session , SystemUser systemUser) {
        Employee employee = (Employee) session.getAttribute("employee");
        systemUser.setUserId(employee.getSystemUser().getUserId());
        Integer count=loginService.updpas(systemUser);
        Map<String, Object> map = new HashMap<>();
        if(count==1){
            map.put("message","");
            session.invalidate();
        }else{
            map.put("message","原密码不对");
        }
        return map;
    }
    @RequestMapping("/selMenuById")
    public Map<String,Object> selMenuById(Integer jobId){
        Map<String, Object> map = new HashMap<>();
        //根据Id查询所拥有的职位
        List<Menu> list= menuService.selMenuById(jobId);
        map.put("list",list);
        return map;
    }
    //修改职位权限
    @RequestMapping("/updMenu")
    public Map<String,Object> updMenu(Integer jobId ,String result){
        Map<String, Object> map = new HashMap<>();
        //根据Id删除所有权限
        Integer count=menuService.delMenuById(jobId);
        //转成list数组
        String[] split = result.split(",");
        List<String> list = Arrays.asList(split);
        //添加权限
        map.put("jobId",jobId);
        map.put("list",list);
        //添加权限信息
        Integer count1=menuService.addMenu(map);
        if(count!=0){
            map.put("message","修改成功");
        }else{
            map.put("message","修改失败");
        }
        return map;
    }
    @RequestMapping("/selMenuAll")
    public Map<String,Object> selMenuAll(){
        Map<String, Object> map = new HashMap<>();
        List<Menu> list= menuService.selMenuAll();
        map.put("list",list);
        return map;
    }
    @RequestMapping("/addMenu")
    public Map<String,Object> addMenu(String jobTitle ,String result){
        Map<String, Object> map = new HashMap<>();
        //先添加职位信息
        Integer jobId=jobService.addJobByName(jobTitle);

        //转成list数组
        String[] split = result.split(",");
        List<String> list = Arrays.asList(split);
        //添加权限
        map.put("jobId",jobId);
        map.put("list",list);

        Integer count=menuService.addMenu(map);
        if(count!=0){
            map.put("message","添加成功");
       }else{
            map.put("message","添加失败");
       }
        return map;
    }
    @Autowired
    @Qualifier("loginServiceImpl")
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
    @Autowired
    @Qualifier("menuServiceImpl")
    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }
    @Autowired
    @Qualifier("jobServiceImpl")
    public void setJobService(JobService jobService) {
        this.jobService = jobService;
    }
}
